package com.ruoyi.web.handle.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.web.domain.GuErrorLog;
import com.ruoyi.web.domain.GuMrGu;
import com.ruoyi.web.domain.GuMrGuDapan;
import com.ruoyi.web.handle.service.GetDataService;
import com.ruoyi.web.handle.service.GetOldDateService;
import com.ruoyi.web.mapper.GuErrorLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@EnableAsync
@RestController
@RequestMapping("/getData")
public class GetDataController extends BaseController {


    @Autowired
    private GetDataService getDataService;
    @Autowired
    private GetOldDateService getOldDateService;
    @Autowired
    private GuErrorLogMapper guErrorLogMapper;

    @GetMapping("/everyDate")
    public String getData() throws InterruptedException {
        //获取涨停跌停股池
        //这个接口支持按时间查找，可以查最近三年的数据
        getDataService.getDtgc();
        //获取大盘数据
        getDataService.getDapan();
        //获取每个股票数据以及每个行业
        //getDataService.getData();
        //获取每股每天的逐笔交易
       // getDataService.getZbjy("000001");
        //获取每天的资金流向数据
       // getDataService.getHsstock("000001",1);
        //K线图，每股的数据
        getDataService.getssJy();
        return "success";
    }


    @GetMapping("/oldDate")
    public String getOldData() {
        AjaxResult a = getOldDateService.getJcDate();
        Map<String,Object> map = (Map<String, Object>) a.get("data");
        List<GuMrGu> guMrGuList = (List<GuMrGu>) map.get("guMrGuDapanList");
        List<GuMrGuDapan>  guMrGuDapanList = (List<GuMrGuDapan>) map.get("guMrGuList");

        for(GuMrGu guMrGu : guMrGuList){
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            getOldDateService.getZjlxDate(guMrGu.getDm());
            getOldDateService.getFsDate(guMrGu.getDm(),guMrGu.getMc());
        }
        getOldDateService.getDtZtDate();

        for(GuMrGuDapan  guMrGuDapan : guMrGuDapanList){
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            getOldDateService.getDaPanDate(guMrGuDapan.getDm(),guMrGuDapan.getMc());
        }
        return  "success";
    }

    /**
     * 获取基础数据  股票 、行业、指数
     * @return
     */

    @GetMapping("/jcdate")
    public String getJcDate() {
        //获取股票基础
        //getOldDateService.getJcDate();
        //获取行业概念
        //getOldDateService.getHy();
        //获取指数概念
        //getOldDateService.getZs();

        return "success";
    }

    @GetMapping("/getZjlxDate")
    public String getZjlxDate() {
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("Content-Type", "application/json;charset=UTF-8");
        //股票列表 http://api.mairuiapi.com/hslt/list/8D57E913-D7A4-4DE8-8E9D-522A095297ED
        //根据股票找相关指数、行业、概念 http://api.mairuiapi.com/hszg/zg/000001/8D57E913-D7A4-4DE8-8E9D-522A095297ED
        //获取每只股票名称及数据
        String url = "http://api.mairuiapi.com/hslt/list/8D57E913-D7A4-4DE8-8E9D-522A095297ED";
        Object obj = restTemplate.getForObject(url, Object.class, headers);
        //将object转换为string
        String userListString = JSON.toJSONString(obj);
        //将string封装为对象集合
        List<GuMrGu> guMrGuList = JSONObject.parseArray(userListString,GuMrGu.class);
        for (GuMrGu guMrGu : guMrGuList) {
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            try {
                getOldDateService.getZjlxDate(guMrGu.getDm());
            } catch (Exception e) {
                GuErrorLog guErrorLog = new GuErrorLog();
                guErrorLog.setId(UUID.randomUUID().toString().replace("-",""));
                guErrorLog.setCreateTime(DateUtils.getNowDate());
                guErrorLog.setRemark("【获取股票资金流接口获取出错】---"+JSON.toJSONString(guMrGu));
                guErrorLogMapper.insertGuErrorLog(guErrorLog);
            }
        }

        return "success";
    }

    /**
     * 获取基础数据  股票 、行业、指数
     * @return
     */

    @GetMapping("/dtztDate")
    public String getDtZtDate() {
        getOldDateService.getDtZtDate();
        return "success";
    }


    /**
     * 股票每天的数据
     * @return
     */

    @GetMapping("/getFsDate")
    public String getFsDate() {
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("Content-Type", "application/json;charset=UTF-8");
        //股票列表 http://api.mairuiapi.com/hslt/list/8D57E913-D7A4-4DE8-8E9D-522A095297ED
        //根据股票找相关指数、行业、概念 http://api.mairuiapi.com/hszg/zg/000001/8D57E913-D7A4-4DE8-8E9D-522A095297ED
        //获取每只股票名称及数据
        String url = "http://api.mairuiapi.com/hslt/list/8D57E913-D7A4-4DE8-8E9D-522A095297ED";
        Object obj = restTemplate.getForObject(url, Object.class, headers);
        //将object转换为string
        String userListString = JSON.toJSONString(obj);
        //将string封装为对象集合
        List<GuMrGu> guMrGuList = JSONObject.parseArray(userListString,GuMrGu.class);
        for (GuMrGu guMrGu : guMrGuList) {
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            try {
                getOldDateService.getFsDate(guMrGu.getDm(),guMrGu.getMc());
            } catch (Exception e) {
                GuErrorLog guErrorLog = new GuErrorLog();
                guErrorLog.setId(UUID.randomUUID().toString().replace("-",""));
                guErrorLog.setCreateTime(DateUtils.getNowDate());
                guErrorLog.setRemark("【获取股票资金流接口获取出错】---"+JSON.toJSONString(guMrGu));
                guErrorLogMapper.insertGuErrorLog(guErrorLog);
            }
        }

        return "success";
    }


    /**
     * 获取基础数据  股票 、行业、指数
     * @return
     */

    @GetMapping("/getTestOne")
    public String getTestOne() {
        getOldDateService.getTestOne();
        return "success";
    }


}
